import json

from django.http import JsonResponse
from django.views import View
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SPUSpecification, SpecificationOption
from apps.meiduo_admin.serializers.specs import SpuModel, SPUSpecificationModel, SpecsOptionsModelSer, SpecsOptionsAll

from apps.meiduo_admin.utils.MeiduoPage import PageNum
from django.core.paginator import Page,Paginator




class SpecsView(ModelViewSet):
    # def get(self,request):
    #     data=SPUSpecification.objects.all()
    #     pagesize=request.GET.get('pagesize')
    #     page=request.GET.get('page')
    #     paginator=Paginator(data,pagesize)
    #
    #     dict_list=[]
    #     for data1 in data:
    #         dict_list.append({
    #             "id": data1.id,
    #             "name": data1.name,
    #             "spu": data1.spu.name,
    #             "spu_id": data1.spu_id
    #         })
    #
    #
    #     return JsonResponse({
    #         'counts':paginator.count,
    #         'lists':dict_list,
    #         "page": page,
    #         "pages": paginator.num_pages,
    #         "pagesize": pagesize
    #
    #     },status=200)


    pagination_class = PageNum
    queryset = SPUSpecification.objects.all()
    serializer_class = SpuModel

    # def post(self,request):
    #     """{
    #     "id": "规格id",
    #     "name": "规格名称",
    #     "spu": "SPU商品名称",
    #     "spu_id": "SPU商品id"
    # }"""
    #     data=json.loads(request.body.decode())
    #     name=data.get('name')
    #     spu_id=data.get('spu_id')
    #     spec=SPUSpecification.objects.create(name=name,spu_id=spu_id)
    #     return JsonResponse({
    #     "id": spec.id,
    #     "name": spec.name,
    #     "spu": spec.spu.name,
    #     "spu_id": spu_id
    #     },status=201)
# class SpecView(View):
#     def get(self,request,pk):




class SPUSpecView(ListAPIView):
    serializer_class =SPUSpecificationModel
    def get_queryset(self):
        pk=self.kwargs.get('pk')
        return SPUSpecification.objects.filter(spu_id=pk)


class SpecsOptionsViewSet(ModelViewSet):
    pagination_class = PageNum
    queryset = SpecificationOption.objects.all()
    serializer_class = SpecsOptionsModelSer

class SpecsOptionsAllView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecsOptionsAll

